package com.darrenchan.leetcode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

/**
 * @Desc
 * @Author chenchi03
 * @CreateTime 2019-12-28 18:27
 */
public class Q144 {
    public List<Integer> preorderTraversal(TreeNode root) {
        Stack<TreeNode> stack = new Stack<>();
        List<Integer> list = new ArrayList<>();
        if(root == null) {
            return list;
        }

        stack.add(root);
        while (!stack.isEmpty()) {
            root = stack.pop();
            list.add(root.val);
            if(root.right != null) {
                stack.add(root.right);
            }

            if(root.left != null) {
                stack.add(root.left);
            }
        }
        return list;
    }
}
